package com.ruoyi.web.controller.weixin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.WechatConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wechat.domain.Code2Session;
import com.ruoyi.wechat.domain.WechatUser;
import com.ruoyi.wechat.service.IWechatLoginService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/wechat")
public class WechatLoginController extends BaseController {


    @Autowired
    private IWechatLoginService wechatLoginService;


    @RequestMapping("/getToken")
    @ResponseBody
    public Map<String,Object> getToken(String code){
        //获取当前会话的sessionId
        String sessionId = (String) SecurityUtils.getSubject().getSession().getId();
        Code2Session code2Session = wechatLoginService.code2Session(code);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("openid",code2Session.getOpenid());
        map.put("sessionId",sessionId);
        return map;
    }


    @RequestMapping("/login")
    @ResponseBody
    public AjaxResult wechatLogin(@RequestParam(value = "openid", required = false) String openid,
                                  @RequestParam(value = "rawData", required = false) String rawData,
                                  @RequestParam(value = "signature", required = false) String signature,
                                  @RequestParam(value = "encrypteData", required = false) String encrypteData,
                                  @RequestParam(value = "iv", required = false) String iv){
        JSONObject rawDataJson = JSON.parseObject(rawData);
        WechatUser user = new WechatUser();
        String nickName = rawDataJson.getString("nickName");
        String avatarUrl = rawDataJson.getString("avatarUrl");
        String gender = rawDataJson.getString("gender");
        String city = rawDataJson.getString("city");
        String country = rawDataJson.getString("country");
        String province = rawDataJson.getString("province");
        user.setOpenid(openid);
        user.setNickName(nickName);
        user.setAvatarUrl(avatarUrl);
        user.setGender(gender);
        user.setCity(city);
        user.setProvince(province);
        user.setCountry(country);
        user.setStatus(WechatConstants.USER_NORMAL);
        WechatUser u = wechatLoginService.getWechatUserByOpenId(openid);
        if(StringUtils.isNull(u)){
             return toAjax(wechatLoginService.insertWechatUser(user));
        }else{
            user.setId(u.getId());
            return toAjax(wechatLoginService.updateWechatUser(user));
        }
    }
}
